//Erstellt von Moritz Liepe
package main.simulation;

import main.IModuleControl;
import main.Module;

import org.eclipse.draw2d.Figure;
import org.eclipse.swt.widgets.Display;


public class SummandBehavior implements IBehavior
{

	@Override
	public void simulate(Module module) 
	{
		boolean bit0 = false;
		boolean bit1 = false;
		boolean bit2 = false;
		boolean bit3 = false;
		
		IModuleControl summand = null;
		
		IModuleControl check0 = null;
		IModuleControl check1 = null;
		IModuleControl check2 = null;
		IModuleControl check3 = null;
		
		IModuleControl out0 = null;
		IModuleControl out1 = null;
		IModuleControl out2 = null;
		IModuleControl out3 = null;
		
		for(int i = 0; i < module.getChildren().size(); i++)
		{
			Figure moduleChild  = (Figure)module.getChildren().get(i);
			if(moduleChild instanceof IModuleControl)
			{
				IModuleControl control = (IModuleControl)moduleChild ;
				if(control.getAddress().equals("summand"))
				{
					summand = control;
					continue;
				}
				if(control.getAddress().equals("out0"))
				{
					out0 = control;
					continue;
				}
				if(control.getAddress().equals("out1"))
				{
					out1 = control;
					continue;
				}
				if(control.getAddress().equals("out2"))
				{
					out2 = control;
					continue;
				}
				if(control.getAddress().equals("out3"))
				{
					out3 = control;
					continue;
				}
				if(control.getAddress().equals("bit0"))
				{
					check0 = control;
					continue;
				}
				if(control.getAddress().equals("bit1"))
				{
					check1 = control;
					continue;
				}
				if(control.getAddress().equals("bit2"))
				{
					check2 = control;
					continue;
				}
				if(control.getAddress().equals("bit3"))
				{
					check3 = control;
					continue;
				}
			}
		}
		
		//Parse
		
		if(check0.getValue().equals(Boolean.toString(true)))
		{
			bit0 = true;
		}
		if(check1.getValue().equals(Boolean.toString(true)))
		{
			bit1 = true;
		}
		if(check2.getValue().equals(Boolean.toString(true)))
		{
			bit2 = true;
		}
		if(check3.getValue().equals(Boolean.toString(true)))
		{
			bit3 = true;
		}
		
		//Set Label
		
		int number 	 = bit0 ? 1 : 0;
		number 		+= bit1 ? 2 : 0;
		number 		+= bit2 ? 4 : 0;
		number 		+= bit3 ? 8 : 0;
		
		if((summand.getValue() == null) || (! summand.getValue().equals(Integer.toString(number))))
		{
			Display.getDefault().asyncExec(new SetControlValue(summand,Integer.toString(number)));
		}
		
		//Set Outputs
		
		if((out0.getValue() == null) || (!out0.getValue().equals(Boolean.toString(bit0))))
		{
			Display.getDefault().asyncExec(new SetControlValue(out0,Boolean.toString(bit0)));
		}
		if((out1.getValue() == null) || (!out1.getValue().equals(Boolean.toString(bit1))))
		{
			Display.getDefault().asyncExec(new SetControlValue(out1,Boolean.toString(bit1)));
		}
		if((out2.getValue() == null) || (!out2.getValue().equals(Boolean.toString(bit2))))
		{
			Display.getDefault().asyncExec(new SetControlValue(out2,Boolean.toString(bit2)));
		}
		if((out3.getValue() == null) || (!out3.getValue().equals(Boolean.toString(bit3))))
		{
			Display.getDefault().asyncExec(new SetControlValue(out3,Boolean.toString(bit3)));
		}
	}

}
